package markan;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ServerLiveCheck {

	/**
	 * @param args
	 */
//	public static void main(String[] args) {
//		ServerLiveCheck serverLiveCheck = new ServerLiveCheck();
//		System.out.println(serverLiveCheck.checkServer());
//		 
//	}  
	
	private String mobileUrl = "http://m.renewableenergy.or.kr/";
	private String portalUrl = "https://www.renewableenergy.or.kr/";
	
	public int checkServer() {
		return checkServer(portalUrl);
	}
	
	/**
	 * 서버 상태를 체크한다.
	 * @param checkUrl
	 * @return
	 */
	public int checkServer(String checkUrl) {
		HttpURLConnection conn = null;
		int resultCode = 0;
		try {
			URL url = new URL(checkUrl);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.setConnectTimeout(1000 * 60);	//60초 (서버 커넥션 시간)
			conn.setReadTimeout(1000 * 60 * 5);		//5분 (페이지 읽어오는 시간)
			conn.setInstanceFollowRedirects(false);
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlendcoded");
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.connect();
	
			resultCode = conn.getResponseCode();
						
		} catch (MalformedURLException me) {
			me.printStackTrace();
			resultCode = 502;
		} catch (IOException ioe) {
			ioe.printStackTrace();
			resultCode = 502;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(conn!=null) try { conn.disconnect(); }catch(Exception e) { }
		}
		
		return resultCode;
	}

}
